package com.auxgroup.smarthome.enterpriseapi.locations;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auxgroup.smarthome.enterpriseapi.responsebody.DevicesLocations;
import java.util.*;

/**
 * Created by kevinchen on 2017/3/25.
 */
public class LocationDistributionUtils {

    public static LocationsCountBean getLocationsCountBean(DevicesLocations devicesLocations){
        if (devicesLocations == null)return null;
        LocationEnCnMaps locationEnCnMaps = ReadLocationJson.readLocationJson();
        Map<String, String> cityMap = locationEnCnMaps.getCityMap();
        Map<String, String> countryMap = locationEnCnMaps.getCountryMap();
        Map<String, String> regionMap = locationEnCnMaps.getRegionMap();
        int total = devicesLocations.getTotal();
        LocationsCountBean locationsCountBean = new LocationsCountBean(total);
        String location = devicesLocations.getLocation();
        JSONObject jsonObject = JSON.parseObject(location);
        Set<Map.Entry<String, Object>> countryEntries = jsonObject.entrySet();
        for(Iterator<Map.Entry<String, Object>> countryIterator = countryEntries.iterator(); countryIterator.hasNext();){
            Map.Entry<String, Object> next = countryIterator.next();
            String countryEn = next.getKey();
            if (countryMap.containsKey(countryEn)){
                String countryCn = cityMap.get(countryEn);
                Object countryValue = next.getValue();
                JSONObject jsonCountryValue =(JSONObject) JSON.toJSON(countryValue);
                int countryCount = jsonCountryValue.getIntValue("count");
                double rate = ((double)countryCount) / ((double)total);
                //System.out.println("国家英文名："+countryEn +" ,国家中文名："+countryCn + ", 设备数量："+countryCount +",比率："+rate);
                CountryCountBean countryCountBean = new CountryCountBean(countryCn, countryEn, countryCount, rate);
                locationsCountBean.addCountryCountBean(countryCountBean);
                Set<Map.Entry<String, Object>> regionEntries = jsonCountryValue.entrySet();
                for(Iterator<Map.Entry<String, Object>> regionIterator = regionEntries.iterator(); regionIterator.hasNext();){
                    Map.Entry<String, Object> next1 = regionIterator.next();
                    String regionEn = next1.getKey();
                    if (regionMap.containsKey(regionEn)){
                        String regionCn = regionMap.get(regionEn);
                        Object regionValue = next1.getValue();

                        JSONObject jsonRegionValue =(JSONObject) JSON.toJSON(regionValue);
                        int regionCount = jsonRegionValue.getIntValue("count");
                        double regionRate = ((double)regionCount) / ((double)countryCount);
                        //System.out.println("省份英文名："+regionEn +" ,省份中文名："+regionCn + ", 设备数量："+regionCount +",比率："+regionRate);
                        RegionCountBean regionCountBean = new RegionCountBean(regionCn, regionEn, regionCount, regionRate);
                        countryCountBean.addRegionCountBean(regionCountBean);
                        Set<Map.Entry<String, Object>> cityEntries = jsonRegionValue.entrySet();
                        for(Iterator<Map.Entry<String, Object>> cityIterator = cityEntries.iterator(); cityIterator.hasNext();) {
                            Map.Entry<String, Object> next2 = cityIterator.next();
                            String cityEn = next2.getKey();
                            if (cityMap.containsKey(cityEn)){
                                String cityCn = cityMap.get(cityEn);
                                Object value = next2.getValue();
                                int cityCount = Integer.parseInt(value.toString());
                                double cityRate = ((double)cityCount) / ((double)regionCount);
                                //System.out.println("城市英文名："+cityEn +" ，城市中文名："+cityCn + ", 设备数量："+cityCount +",比率："+cityRate);
                                CityCountBean cityCountBean = new CityCountBean(cityCn, cityEn, cityCount, cityRate);
                                regionCountBean.addCitycountBean(cityCountBean);
                            }
                        }

                    }
                }

            }
        }
        return locationsCountBean;
    }


    public static LocationsCountBean getEnterpriseLocationsCountBean(List<LocationsCountBean> locationsCountBeans) {
        int total = 0;
//        DevicesLocations devicesLocations1 = new DevicesLocations();
        //地域组合(返回结果)
        LocationsCountBean sum = new LocationsCountBean(total);
        //国家组合(返回结果)
        List<CountryCountBean> ccbList = new ArrayList<>();
        //记录国家/省份/城市重复出现次数用于求比率
        Map<String, Integer> appearCount = new HashMap();
        //遍历集合()
        for (LocationsCountBean lcb : locationsCountBeans) {
            //全地域数量
            sum.setTotal(sum.getTotal() + lcb.getTotal());
            //国家集合
            for (CountryCountBean ccb : lcb.getCountryCounts()) {
                for (CountryCountBean ccbSum : ccbList) {
                    if (ccb.getCountryEn().equals(ccbSum.getCountryEn())) {
                        Integer i = 0;
                        appearCount.put(ccb.getCountryEn(), ++i);
                        //结果集合有对应国家
                        ccbSum.setCount(ccbSum.getCount() + ccb.getCount());
                        ccbSum.setRate(ccbSum.getRate() + ccb.getRate());
                        //省份组合(返回结果)
                        List<RegionCountBean> rcbList = new ArrayList<>();
                        for (RegionCountBean rcb : ccb.getRegionCounts()) {
                            for (RegionCountBean rcbSum : rcbList) {
                                if (rcb.getRegionEn().equals(rcbSum.getRegionEn())) {
                                    Integer j = 0;
                                    appearCount.put(rcb.getRegionEn(), ++j);
                                    //结果集合有对应省份
                                    rcbSum.setCount(ccbSum.getCount() + ccb.getCount());
                                    rcbSum.setRate(rcbSum.getRate() + rcb.getRate());
                                    //城市集合(返回结果)
                                    List<CityCountBean> cityList = new ArrayList<>();
                                    for (CityCountBean city : rcb.getCityCounts()) {
                                        for (CityCountBean citySum : cityList) {
                                            if (rcb.getRegionEn().equals(rcbSum.getRegionEn())) {
                                                Integer k = 0;
                                                appearCount.put(city.getCityEn(), ++k);
                                                //结果集合有对应城市
                                                citySum.setCount(citySum.getCount() + city.getCount());
                                                citySum.setRate(citySum.getRate() + city.getRate());
                                            }
                                        }
                                        //结果集合没有对应省份
                                        cityList.add(city);
                                    }
                                    rcb.setCityCounts(cityList);
                                }
                            }
                            //结果集合没有对应省份
                            rcbList.add(rcb);
                        }
                        ccb.setRegionCounts(rcbList);
                    }
                }
                //结果集合没有对应国家
                ccbList.add(ccb);
            }
            sum.setCountryCounts(ccbList);
        }
        //处理比例rate
        //遍历集合()
        for (Map.Entry<String, Integer> e : appearCount.entrySet()) {
            for (LocationsCountBean lcb : locationsCountBeans) {
                //国家集合
                for (CountryCountBean ccb : lcb.getCountryCounts()) {
                    if (e.getKey().equals(ccb.getCountryEn())) {
                        ccb.setRate(ccb.getRate() / e.getValue());
                    }
                    //省份组合(返回结果)
                    for (RegionCountBean rcb : ccb.getRegionCounts()) {
                        if (e.getKey().equals(rcb.getRegionEn())) {
                            rcb.setRate(rcb.getRate() / e.getValue());
                        }
                        //城市集合(返回结果)
                        for (CityCountBean city : rcb.getCityCounts()) {
                            if (e.getKey().equals(city.getCityEn())) {
                                city.setRate(city.getRate() / e.getValue());
                            }
                        }
                    }
                }
            }
        }
        return sum;
    }
}
